<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="" prop="productno">
      <el-input v-model="dataForm.productno" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="serialno">
      <el-input v-model="dataForm.serialno" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="location">
      <el-input v-model="dataForm.location" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="container">
      <el-input v-model="dataForm.container" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="tolocation">
      <el-input v-model="dataForm.tolocation" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="tocontainer">
      <el-input v-model="dataForm.tocontainer" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="lastupdateon">
      <el-input v-model="dataForm.lastupdateon" placeholder=""></el-input>
    </el-form-item>
    <el-form-item label="" prop="type">
      <el-input v-model="dataForm.type" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          productno: '',
          serialno: '',
          location: '',
          container: '',
          tolocation: '',
          tocontainer: '',
          lastupdateon: '',
          type: ''
        },
        dataRule: {
          productno: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          serialno: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          location: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          container: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          tolocation: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          tocontainer: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          lastupdateon: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          type: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/poschangedata/poschangedatacopy1/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.productno = data.poschangeDataCopy1.productno
                this.dataForm.serialno = data.poschangeDataCopy1.serialno
                this.dataForm.location = data.poschangeDataCopy1.location
                this.dataForm.container = data.poschangeDataCopy1.container
                this.dataForm.tolocation = data.poschangeDataCopy1.tolocation
                this.dataForm.tocontainer = data.poschangeDataCopy1.tocontainer
                this.dataForm.lastupdateon = data.poschangeDataCopy1.lastupdateon
                this.dataForm.type = data.poschangeDataCopy1.type
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/poschangedata/poschangedatacopy1/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'productno': this.dataForm.productno,
                'serialno': this.dataForm.serialno,
                'location': this.dataForm.location,
                'container': this.dataForm.container,
                'tolocation': this.dataForm.tolocation,
                'tocontainer': this.dataForm.tocontainer,
                'lastupdateon': this.dataForm.lastupdateon,
                'type': this.dataForm.type
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
